/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.bean.LicenseInfo;
import com.minierp.bean.PurchaseBillItemBean;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.Establishment;
import com.minierp.model.PurchaseBill;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class PurchaseBillListForAccountExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"PurchaseBillListForAccount.xls\"");
		HSSFFont titltFont = workbook.createFont();
		titltFont.setFontHeightInPoints(16);
		titltFont.setBoldweight(700);
		HSSFFont boldFont = workbook.createFont();
		boldFont.setBoldweight(700);
		HSSFFont normalFont = workbook.createFont();
		HSSFCellStyle titleCellStyle = workbook.createCellStyle();
		titleCellStyle.setFont(titltFont);
		titleCellStyle.setAlignment(2);
		titleCellStyle.setVerticalAlignment(1);
		HSSFCellStyle redDataCellStyle = workbook.createCellStyle();
		HSSFFont redFont = workbook.createFont();
		redFont.setColor(10);
		redDataCellStyle.setFont(redFont);
		redDataCellStyle.setBorderBottom(1);
		redDataCellStyle.setBorderTop(1);
		redDataCellStyle.setBorderLeft(1);
		redDataCellStyle.setBorderRight(1);
		HSSFCellStyle norDataCellStyle = workbook.createCellStyle();
		norDataCellStyle.setBorderBottom(1);
		norDataCellStyle.setBorderTop(1);
		norDataCellStyle.setBorderLeft(1);
		norDataCellStyle.setBorderRight(1);
		HSSFCellStyle boldCellStyle = workbook.createCellStyle();
		boldCellStyle.setFont(boldFont);
		boldCellStyle.setBorderBottom(1);
		boldCellStyle.setBorderTop(1);
		boldCellStyle.setBorderLeft(1);
		boldCellStyle.setBorderRight(1);
		HSSFCellStyle rightCellStyle = workbook.createCellStyle();
		rightCellStyle.setAlignment(3);
		Establishment est = (Establishment) model.get("est");
		LicenseInfo lif = EncCommonHelper.getLicenseInfo(request);
		HSSFSheet sheet = workbook.createSheet("list");
		byte colNum = 9;
		HSSFRow r = sheet.createRow(0);
		r.setHeight(600);
		HSSFCell c = r.createCell(0);

		for (int ts = 1; ts < colNum; ++ts) {
			r.createCell((short) ts);
		}

		c.setCellStyle(titleCellStyle);
		c.setCellValue(new HSSFRichTextString(lif.getEnterpriseName()
				+ " 采购对帐单"));
		sheet.addMergedRegion(new Region(0, 0, 0, (short) (colNum - 1)));
		r = sheet.createRow(1);
		c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		HSSFRichTextString arg27 = new HSSFRichTextString("供应商名称："
				+ est.getEstName());
		arg27.applyFont(0, 6, boldFont);
		arg27.applyFont(6, arg27.length(), normalFont);
		c.setCellValue(arg27);
		sheet.addMergedRegion(new Region(1, 0, 1, 2));
		c = r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		arg27 = new HSSFRichTextString("起止日期：" + model.get("start_date")
				+ " 至 " + model.get("end_date"));
		arg27.applyFont(0, 5, boldFont);
		arg27.applyFont(5, arg27.length(), normalFont);
		c.setCellValue(arg27);
		sheet.addMergedRegion(new Region(1, 3, 1, 5));
		c = r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		arg27 = new HSSFRichTextString("采购金额：" + model.get("costTotal"));
		arg27.applyFont(0, 5, boldFont);
		arg27.applyFont(5, arg27.length(), normalFont);
		c.setCellValue(arg27);
		sheet.addMergedRegion(new Region(1, 6, 1, 8));
		r = sheet.createRow(2);
		c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		arg27 = new HSSFRichTextString("已付款金额：" + model.get("paiedTotal"));
		arg27.applyFont(0, 6, boldFont);
		arg27.applyFont(6, arg27.length(), normalFont);
		c.setCellValue(arg27);
		sheet.addMergedRegion(new Region(2, 0, 2, 2));
		c = r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		arg27 = new HSSFRichTextString("已调账金额：" + model.get("adjustTotal"));
		arg27.applyFont(0, 6, boldFont);
		arg27.applyFont(6, arg27.length(), normalFont);
		c.setCellValue(arg27);
		sheet.addMergedRegion(new Region(2, 3, 2, 5));
		c = r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		arg27 = new HSSFRichTextString("尚欠金额：" + model.get("remainTotal"));
		arg27.applyFont(0, 5, boldFont);
		arg27.applyFont(5, arg27.length(), normalFont);
		c.setCellValue(arg27);
		sheet.addMergedRegion(new Region(2, 6, 2, 8));
		r = sheet.createRow(3);
		c = r.createCell(0);

		for (int list = 1; list < colNum; ++list) {
			r.createCell((short) list);
		}

		c.setCellValue(new HSSFRichTextString(""));
		sheet.addMergedRegion(new Region(3, 0, 3, (short) (colNum - 1)));
		r = sheet.createRow(4);
		c = r.createCell(0);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("单号"));
		c = r.createCell(1);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("日期"));
		c = r.createCell(2);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("产品编号"));
		c = r.createCell(3);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("品名规格"));
		c = r.createCell(4);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("单位"));
		c = r.createCell(5);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("数量"));
		c = r.createCell(6);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("单价"));
		c = r.createCell(7);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("金额"));
		c = r.createCell(8);
		c.setCellStyle(boldCellStyle);
		c.setCellValue(new HSSFRichTextString("备注"));
		List arg28 = (List) model.get("list");
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

		int os;
		for (os = 0; os < arg28.size(); ++os) {
			Object[] objs = (Object[]) arg28.get(os);
			PurchaseBill b = (PurchaseBill) objs[0];
			PurchaseBillItemBean item = (PurchaseBillItemBean) objs[1];
			String paied = (String) objs[2];
			r = sheet.createRow(os + 5);
			c = r.createCell(0);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			c.setCellValue(new HSSFRichTextString(b.getBillCode()));
			c = r.createCell(1);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			c.setCellValue(new HSSFRichTextString(
					sf.format(b.getPurchaseDate())));
			c = r.createCell(2);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			c.setCellValue(new HSSFRichTextString(item.getProductCode()));
			c = r.createCell(3);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			c.setCellValue(new HSSFRichTextString(item.getProductName()));
			c = r.createCell(4);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			c.setCellValue(new HSSFRichTextString(item.getUnit()));
			c = r.createCell(5);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			if (b.getBillType().equals("BUY")) {
				c.setCellValue(item.getPurchaseBillItem().getAmount()
						.doubleValue());
			} else {
				c.setCellValue((new BigDecimal(0)).subtract(
						item.getPurchaseBillItem().getAmount()).doubleValue());
			}

			c = r.createCell(6);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			c.setCellValue(item.getPurchaseBillItem().getUnitPrice()
					.doubleValue());
			c = r.createCell(7);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			if (b.getBillType().equals("BUY")) {
				c.setCellValue(item.getPurchaseBillItem().getCost()
						.doubleValue());
			} else {
				c.setCellValue((new BigDecimal(0)).subtract(
						item.getPurchaseBillItem().getCost()).doubleValue());
			}

			c = r.createCell(8);
			if (paied.equals("Y")) {
				c.setCellStyle(redDataCellStyle);
			} else {
				c.setCellStyle(norDataCellStyle);
			}

			c.setCellValue(new HSSFRichTextString(item.getPurchaseBillItem()
					.getMark()));
		}

		r = sheet.createRow(arg28.size() + 5);
		c = r.createCell(0);

		for (os = 1; os < colNum; ++os) {
			r.createCell((short) os);
		}

		c.setCellStyle(rightCellStyle);
		c.setCellValue(new HSSFRichTextString("小计金额：" + model.get("costTotal")));
		sheet.addMergedRegion(new Region(arg28.size() + 5, 0, arg28.size() + 5,
				(short) (colNum - 1)));
		sheet.setColumnWidth(0, 5000);
		sheet.setColumnWidth(1, 3200);
		ServletOutputStream arg29 = response.getOutputStream();
		workbook.write(arg29);
		arg29.flush();
		arg29.close();
	}
}